package com.crawling.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.crawling.mapper.*;
import com.crawling.model.*;
import com.crawling.service.CrawlingBeginService;
import com.crawling.service.CreatePhotoService;
import com.crawling.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lr
 * @since 2018-09-22
 */
@Service
@Slf4j
public class CrawlingBeginServiceImpl implements CrawlingBeginService {
    @Autowired
    private CrawlingBigPicMapper crawlingBigPicMapper;
    @Autowired
    private CrawlingBigTabMapper crawlingBigTabMapper;
    @Autowired
    private CrawlingPicTagsMapper crawlingPicTagsMapper;
    @Autowired
    private CrawlingPicThumbnailMapper crawlingPicThumbnailMapper;
    @Autowired
    private CrawlingTabInnerMapper crawlingTabInnerMapper;
    @Autowired
    private CrawlingTagsThumbnailMapper crawlingTagsThumbnailMapper;
    @Autowired
    private CrawlingPeopleNameMapper crawlingPeopleNameMapper;

    @Autowired
    CreatePhotoService createPhotoService;


    private static String IMAGE_PATH = "D:/yitu/";
    private static boolean IS_GO_ON = true;


    @Override
    public String start(String key1, String key2) {
        String URL = "https://www.yeitu.com/";
        return getHeadNavBigTab(URL, key1, key2);
    }

    @Override
    public Object one(String url) {
        CrawlingPicThumbnail crawlingPicThumbnail = new CrawlingPicThumbnail();
        crawlingPicThumbnail.setPeopleName("no");
        crawlingPicThumbnail.setPicHrefText(url);
        crawlingPicThumbnailMapper.insertSelective(crawlingPicThumbnail);
        CrawlingBeginService proxy = getThisProxy();
        Future<List<CrawlingBigPic>> bigPics = proxy.downloadEveryPeoleImgage(url, crawlingPicThumbnail);
        try {
            List<CrawlingBigPic> res = bigPics.get();
            // 插入到其他表
            createPhotoService.createPhoto(crawlingPicThumbnail, res, threadLocal.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    private CrawlingBeginService getThisProxy() {
        return SpringUtil.getBean(CrawlingBeginService.class);
    }


    //获取首页上的大tab $(".head_nav>ul>li").eq(0).children("a").text();
    public String getHeadNavBigTab(String URL, String key1, String key2) {
        //共有7个大tab需要爬取
        String html = HttpClientUtil.get(URL);
        Document document = Jsoup.parse(html);
        //页面上每个 小tab名称和内容
        Elements elements = document.select(".head_nav>ul>li");
        int count = 0;
        for (Element e : elements) {
            if (count > 0 && count < 8) {
                String text = e.select("a").text();
                String href = e.select("a").attr("href");
                log.info(text + "================" + href);

                CrawlingBigTab crawlingBigTab = new CrawlingBigTab();
                crawlingBigTab.setAttributeLink(URL);
                crawlingBigTab.setTabHref(href);
                crawlingBigTab.setTabIndex(count);
                crawlingBigTab.setTabName(text);
                CrawlingBigTab getCrawlingBigTab = crawlingBigTabMapper.selectOne(crawlingBigTab);
                if (!StringUtils.containsIgnoreCase(href, key1)) {
                    continue;
                }
                if (getCrawlingBigTab == null) {
                    crawlingBigTab.setCreateTime(new Date());
                    crawlingBigTabMapper.insertSelective(crawlingBigTab);
                    System.out.println(crawlingBigTab.toString());
                    getUrlTitleAndImage(href, crawlingBigTab, key2);
                } else {
                    getUrlTitleAndImage(href, getCrawlingBigTab, key2);
                }
            }
            count++;
        }
        return count + "个tab正在爬取";
    }


    //获取到每一页上的 小tab
    public String getUrlTitleAndImage(String URL, CrawlingBigTab crawlingBigTab, String key2) {
        //$(".row").children("div").children("ul").children("li").eq(0).find("a").text();
        try {
            if (IS_GO_ON) {

                String html = HttpClientUtil.get(URL);
                Document document = Jsoup.parse(html);
                //页面上每个 小tab名称和内容
                Elements elements = document.select(".row").select("div").select("ul").select("li");
                int count = 1;
                for (Element e : elements) {

                    String text = e.select("a").text();
                    String href = e.select("a").attr("href");
                    log.info(text + "================" + href);

                    CrawlingTabInner crawlingTabInner = new CrawlingTabInner();
                    crawlingTabInner.setBigTabName(crawlingBigTab.getTabName());
                    crawlingTabInner.setTabHref(href);
                    // 第二个关键词
                    if (!StringUtils.containsIgnoreCase(href, key2)) {
                        continue;
                    }
                    CrawlingTabInner getCrawlingTabInner = crawlingTabInnerMapper.selectOne(crawlingTabInner);
                    if (getCrawlingTabInner != null) {
                        getInnerMessage(href, getCrawlingTabInner);
                    } else {
                        crawlingTabInner.setCreateTime(new Date());
                        crawlingTabInner.setBigTabId(crawlingBigTab.getBigTabId());
                        crawlingTabInner.setTabName(text);
                        crawlingTabInner.setTabIndex(count);
                        crawlingTabInnerMapper.insertSelective(crawlingTabInner);
                        count++;
                        getInnerMessage(href, crawlingTabInner);
                    }
                }
                return "爬取已经开始";

            }
        } catch (Exception e) {
            log.error("页面解析错误 ：{}", e);
            return "页面解析错误";
        }
        return "爬取已停止 IS_GO_ON:" + IS_GO_ON;
    }

    public void getInnerMessage(String href, CrawlingTabInner crawlingTabInner) {
        //每一个单独模块的连接地址 如：https://www.yeitu.com/meinv/xinggan/
        //FIXMED 修改过的通用方法 $(".w,.row>div>ul").children("li[class='image-box']");
        try {
            String htmlInner = HttpClientUtil.get(href);
            Document documentInner = Jsoup.parse(htmlInner);
            Elements elementsInner = documentInner.select(".w,.row>div>ul").select("li[class='image-box']");

            //获取其他页面的数据
            //first 获取总页数     $("#pages").children("a")
            Elements elementsPages = documentInner.select("#pages").select("a");
            int bigPge;
            List<Integer> getAllPages = new LinkedList<>();
            for (Element page : elementsPages) {
                String onepage = page.text() == null ? "1" : page.text().trim();
                if (isInteger(onepage)) {
                    getAllPages.add(Integer.parseInt(onepage));
                }
            }
            if (getAllPages.size() == 0) {
                bigPge = 0;
            } else {
                Object[] objects = getAllPages.toArray();
                Arrays.sort(objects);
                bigPge = Integer.parseInt(String.valueOf(objects[objects.length - 1]));
            }

            //第一页的数据
            downOnePartPageAllImages(elementsInner, href, crawlingTabInner, 1);

            //然后循环拼接请求的路径
            if (bigPge > 1) {
                for (int i = 2; i < bigPge; i++) {
                    final String otherHref = href + i + ".html";
                    String htmlInnerOtherHref = HttpClientUtil.get(otherHref);
                    Document documentInnerOtherHref = Jsoup.parse(htmlInnerOtherHref);
                    Elements elementsInnerOther = documentInnerOtherHref.select(".w,.row>div>ul").select("li[class='image-box']");
                    //其他页面的缩略图
                    downOnePartPageAllImages(elementsInnerOther, otherHref, crawlingTabInner, i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("单个模块页面解析错误：{}", e);
        }
    }

    //下载缩略图部分
    public void downOnePartPageAllImages(Elements elementsInner, final String href, CrawlingTabInner crawlingTabInner, int pageIndex) {
        try {

            //单独模块的每个图片缩略图和标题等
            int indexOrder = 1;
            for (final Element eInner : elementsInner) {

                CrawlingPicThumbnail crawlingPicThumbnail = new CrawlingPicThumbnail();

                final String image = eInner.select("a").select("img").attr("data-echo");
                String alt = eInner.select("a").select("img").attr("alt");
                String width = eInner.select("a").select("img").attr("width");
                String height = eInner.select("a").select("img").attr("height");
                log.info(alt + "=========" + image);

                int finalIndexOrder = indexOrder;

                //循环打印所有tags
                StringBuilder tags = new StringBuilder();
                Elements tageElements = eInner.select(".tags").select("a");
                //tags.append("【");
                List<CrawlingPicTags> everyOneTags = new ArrayList<>(10);
                String name = null;
                for (Element tag : tageElements) {
                    CrawlingPicTags crawlingPicTag;
                    if (StringUtils.isBlank(name)) {
                        name = tag.text();
                        crawlingPicTag = insetTag(tag, name);
                    } else {
                        crawlingPicTag = insetTag(tag);
                    }
                    everyOneTags.add(crawlingPicTag);
                    tags.append(tag.text().trim());
                    if (tag != tageElements.last()) {
                        tags.append("|");
                    }
                }
                //tags.append("】");
                log.info("单人所有标签：" + tags.toString());

                crawlingPicThumbnail.setAttributeLink(href);
                crawlingPicThumbnail.setPicHref(image);
                //缩略图和链接都相同说明是一个
                CrawlingPicThumbnail getCrawlingPicThumbnailHref = crawlingPicThumbnailMapper.selectOne(crawlingPicThumbnail);

                crawlingPicThumbnail.setPeopleName(everyOneTags.get(0).getTagName());
                crawlingPicThumbnail.setPicHrefText(alt);
                crawlingPicThumbnail.setTabInnerId(crawlingTabInner.getTabInnerId());

                CrawlingPicThumbnail getCrawlingPicThumbnail = crawlingPicThumbnailMapper.selectOne(crawlingPicThumbnail);


                crawlingPicThumbnail.setPicIndexOrder(finalIndexOrder);

                crawlingPicThumbnail.setTabInnerName(crawlingTabInner.getTabName());
                crawlingPicThumbnail.setTags(tags.toString());
                crawlingPicThumbnail.setThumbnailPageIndex(pageIndex);
                crawlingPicThumbnail.setThumbnailPageSize(elementsInner.size());
                crawlingPicThumbnail.setPicWidth(width);
                crawlingPicThumbnail.setPicHeight(height);
                crawlingPicThumbnail.setCreateTime(new Date());
                //这里未有此条记录去下载图片
                String local_href = getImagePathAndName(href, image);
                //这里插入人名
                CrawlingPeopleName crawlingPeopleName = this.createPeople(name);
                if (crawlingPeopleName != null) {
                    crawlingPicThumbnail.setPeopleId(crawlingPeopleName.getPeopleId());
                }


                //保存缩略图
                if (getCrawlingPicThumbnail == null) {
                    //没有缩略图 插入
                    if (getCrawlingPicThumbnailHref != null && getCrawlingPicThumbnailHref.getLocalHref() != null) {
                        crawlingPicThumbnail.setLocalHref(getCrawlingPicThumbnailHref.getLocalHref());
                    } else {
                        String result = HttpClientUtil.downloadFile(image, local_href);
                        log.info("缩略图下载结果：{}", result);
                        if (!result.equals(HttpClientUtil.download)) {
                            crawlingPicThumbnail.setLocalHref(StringUtils.substringAfter(local_href, IMAGE_PATH));
                        }
                    }
                    crawlingPicThumbnailMapper.insertSelective(crawlingPicThumbnail);

                    for (CrawlingPicTags crawlingPicTags : everyOneTags) {
                        CrawlingTagsThumbnail crawlingTagsThumbnail = new CrawlingTagsThumbnail();
                        if (crawlingPicTags == null || crawlingPicTags.getTagId() == null || crawlingPicThumbnail.getPicThumbnailId() == null) {
                            continue;
                        }
                        crawlingTagsThumbnail.setTagId(crawlingPicTags.getTagId());
                        crawlingTagsThumbnail.setPicThumbnailId(crawlingPicThumbnail.getPicThumbnailId());
                        CrawlingTagsThumbnail getCrawlingTagsThumbnail = crawlingTagsThumbnailMapper.selectOne(crawlingTagsThumbnail);
                        if (getCrawlingTagsThumbnail == null) {
                            crawlingTagsThumbnail.setCreateTime(new Date());
                            crawlingTagsThumbnailMapper.insertSelective(crawlingTagsThumbnail);
                        }
                    }

                    //  这里下载对应缩略图的详细大图片 说明没有此缩略图 说明对应大图没爬取过
                    final String evertPeopleAllBIgImageHref = eInner.select("a").attr("href");
                    log.info("获取的单人大图链接地址：" + evertPeopleAllBIgImageHref);
                    //保存下载图片路径到blog
                    CrawlingBeginService proxy = getThisProxy();
                    Future<List<CrawlingBigPic>> bigPics = proxy.downloadEveryPeoleImgage(evertPeopleAllBIgImageHref, crawlingPicThumbnail);

                    createPhotoService.createPhoto(crawlingPicThumbnail, bigPics.get(), everyOneTags);

                } else {
                    //已经存在此条记录，
                    if (StringUtils.isBlank(getCrawlingPicThumbnail.getLocalHref())) {
                        //这里再次尝试下载
                        String result = HttpClientUtil.downloadFile(image, local_href);
                        log.info("缩略图下载结果：{}", result);
                        if (!result.equals(HttpClientUtil.download)) {
                            crawlingPicThumbnail.setLocalHref(StringUtils.substringAfter(local_href, IMAGE_PATH));
                        }
                    }

                    if (StringUtils.isNotBlank(crawlingPicThumbnail.getLocalHref())) {
                        CrawlingPicThumbnail crawlingPicThumbnailUpdate = new CrawlingPicThumbnail();
                        crawlingPicThumbnailUpdate.setPicThumbnailId(getCrawlingPicThumbnail.getPicThumbnailId());
                        crawlingPicThumbnailUpdate.setLocalHref(crawlingPicThumbnail.getLocalHref());
                        crawlingPicThumbnailUpdate.setUpdateTime(new Date());
                        crawlingPicThumbnailMapper.updateByPrimaryKeySelective(crawlingPicThumbnailUpdate);
                    }
                    if (StringUtils.isBlank(getCrawlingPicThumbnail.getPicHrefText())) {
                        CrawlingPicThumbnail crawlingPicThumbnailUpdate = new CrawlingPicThumbnail();
                        crawlingPicThumbnailUpdate.setPicThumbnailId(getCrawlingPicThumbnail.getPicThumbnailId());
                        crawlingPicThumbnailUpdate.setPeopleName(everyOneTags.get(0).getTagName());
                        crawlingPicThumbnailUpdate.setPicHrefText(alt);
                        crawlingPicThumbnailUpdate.setTabInnerId(crawlingTabInner.getTabInnerId());
                        crawlingPicThumbnailUpdate.setPicIndexOrder(finalIndexOrder);
                        crawlingPicThumbnailUpdate.setTabInnerName(crawlingTabInner.getTabName());
                        crawlingPicThumbnailUpdate.setTags(tags.toString());
                        crawlingPicThumbnailUpdate.setThumbnailPageIndex(pageIndex);
                        crawlingPicThumbnailUpdate.setThumbnailPageSize(elementsInner.size());
                        crawlingPicThumbnailUpdate.setPicWidth(width);
                        crawlingPicThumbnailUpdate.setPicHeight(height);
                        crawlingPicThumbnailUpdate.setUpdateTime(new Date());
                        crawlingPicThumbnailMapper.updateByPrimaryKeySelective(crawlingPicThumbnailUpdate);
                    }
                }

                indexOrder++;
            }
        } catch (Exception e) {
            log.error("缩略图下载失败");
        }
    }

    private static ReentrantLock peopleLock = new ReentrantLock();

    private CrawlingPeopleName createPeople(String name) {
        try {
            peopleLock.lock();
            CrawlingPeopleName crawlingPeopleName = new CrawlingPeopleName();
            crawlingPeopleName.setPeopleName(name);
            CrawlingPeopleName getCrawlingPeopleNameMapper = crawlingPeopleNameMapper.selectOne(crawlingPeopleName);
            if (getCrawlingPeopleNameMapper != null) {
                crawlingPeopleName.setPeopleId(getCrawlingPeopleNameMapper.getPeopleId());
            } else {
                crawlingPeopleName.setCreateTime(new Date());
                crawlingPeopleNameMapper.insert(crawlingPeopleName);
            }
            return crawlingPeopleName;
        } catch (Exception e) {
            log.error("保存人名错误：{},异常信息：{}", name, e);
        } finally {
            peopleLock.unlock();
        }
        return null;
    }


    private static ReentrantLock tagLock = new ReentrantLock();

    public CrawlingPicTags insetTag(Element tag, String... name) {
        Example example = new Example(CrawlingPicTags.class);
        if (StringUtils.isNotBlank(tag.text())) {
            example.createCriteria().andEqualTo("tagName", tag.text());
            tagLock.lock();
            try {
                if (IS_GO_ON) {
                    List<CrawlingPicTags> getTags = crawlingPicTagsMapper.selectByExample(example);
                    if (getTags.size() > 1) {
                        //说明数据集 已经插入多余重复的；
                        log.error("标签数据集 已经插入多余重复的 tag : {}", tag.text());
                    } else {
                        if (getTags.size() == 1) {
                            CrawlingPicTags updateTag = new CrawlingPicTags();
                            updateTag.setTagId(getTags.get(0).getTagId());
                            updateTag.setUpdateTime(new Date());
                            updateTag.setTagAppearNum(getTags.get(0).getTagAppearNum() + 1);
                            crawlingPicTagsMapper.updateByPrimaryKeySelective(updateTag);
                            return getTags.get(0);
                        }
                        CrawlingPicTags crawlingPicTags = new CrawlingPicTags();
                        crawlingPicTags.setCreateTime(new Date());
                        crawlingPicTags.setTagName(tag.text());
                        crawlingPicTags.setTagAppearNum((getTags.size() == 0) ? 1 : (getTags.get(0).getTagAppearNum() + 1));
                        crawlingPicTags.setIsPeople(name.length > 0 ? "y" : "n");
                        crawlingPicTagsMapper.insertSelective(crawlingPicTags);
                        return crawlingPicTags;
                    }
                }
            } catch (Exception e) {
                log.error("插入标签错误：{}", e);
                IS_GO_ON = false; //设置为不继续插入
            } finally {
                tagLock.unlock();
            }
        }
        return null;
    }

    ThreadLocal<List<CrawlingPicTags>> threadLocal = new ThreadLocal<>();

    @Async
    @Override
    public Future<List<CrawlingBigPic>> downloadEveryPeoleImgage(String everyImageUrl, CrawlingPicThumbnail crawlingPicThumbnail) {
        //$(".img_box").children("a").children("img").attr("alt"); 单个图片链接
        // $(".imageset").children("span").eq(1).text();
        //路径 实例 everyImageUrl = "https://www.yeitu.com/meinv/xinggan/20180919_14722.html";
        List<CrawlingBigPic> bigPics = new ArrayList<>();
        try {

            int allPage;
            String htmlInner = HttpClientUtil.get(everyImageUrl);
            Document documentInner = Jsoup.parse(htmlInner);
            String textPage = documentInner.select(".imageset").select("span").eq(1).text();
            String[] pages = textPage.split("/");
            allPage = Integer.parseInt(pages[pages.length - 1].trim());
            log.info(crawlingPicThumbnail.getPeopleName() + ": 一共" + allPage + "页");
            //如果是单个获取，需要额外 获取所有tag
            if ("no".equals(crawlingPicThumbnail.getPeopleName())) {
                List<CrawlingPicTags> tags = insertTags(crawlingPicThumbnail, documentInner);
                threadLocal.set(tags);
            }

            //这里获取第一页
            //alt 其他获取方法 $(".w,.row").children("#title").children("h1").text();
            String firstAlt = documentInner.select(".img_box").select("a").select("img").attr("alt");
            String firstImage = documentInner.select(".img_box").select("a").select("img").attr("src");
            log.info("============第一页============");
            log.info("详细大图片 请求URL:" + everyImageUrl);
            log.info(firstAlt + "=======" + firstImage);
            CrawlingBigPic pic = insertCrawlingBigPic(everyImageUrl, 1, firstImage, firstAlt, allPage, crawlingPicThumbnail);
            bigPics.add(pic);
            for (int i = 2; i < allPage; i++) {

                //第一页之后的
                final String oneUrl = StringUtils.substringBeforeLast(everyImageUrl, ".html") + "_" + i + ".html";
                log.info("请求路径: " + oneUrl);

                String htmlInnerOther = HttpClientUtil.get(oneUrl);
                Document documentInnerOther = Jsoup.parse(htmlInnerOther);
                String oneAlt = documentInnerOther.select(".img_box").select("a").select("img").attr("alt");
                final String oneImageHref = documentInnerOther.select(".img_box").select("a").select("img").attr("src");
                log.info(oneAlt + "=======" + oneImageHref);

                CrawlingBigPic pic2 = insertCrawlingBigPic(oneUrl, i, oneImageHref, oneAlt, allPage, crawlingPicThumbnail);
                bigPics.add(pic2);
            }
        } catch (Exception e) {
            log.error("单张大图片页面解析错误 ：{}", e);
        }
        return new AsyncResult<>(bigPics);
    }

    private List<CrawlingPicTags> insertTags(CrawlingPicThumbnail crawlingPicThumbnail, Document documentInner) {
        // $(".right_content .related_tag a").eq(1).text();
        //循环打印所有tags
        StringBuilder tags = new StringBuilder();
        Elements tageElements = documentInner.select(".right_content .related_tag a");
        //tags.append("【");
        List<CrawlingPicTags> everyOneTags = new ArrayList<>(10);
        String name = null;
        for (Element tag : tageElements) {
            //保存标签
            CrawlingPicTags crawlingPicTag;
            if (StringUtils.isBlank(name)) {
                name = tag.text();
                crawlingPicTag = insetTag(tag, name);
            } else {
                crawlingPicTag = insetTag(tag);
            }
            everyOneTags.add(crawlingPicTag);
            tags.append(tag.text().trim());
            if (tag != tageElements.last()) {
                tags.append("|");
            }
        }
        //tags.append("】");
        log.info("单人所有标签：" + tags.toString());
        for (CrawlingPicTags crawlingPicTags : everyOneTags) {
            CrawlingTagsThumbnail crawlingTagsThumbnail = new CrawlingTagsThumbnail();
            crawlingTagsThumbnail.setTagId(crawlingPicTags.getTagId());
            crawlingTagsThumbnail.setPicThumbnailId(crawlingPicThumbnail.getPicThumbnailId());
            CrawlingTagsThumbnail getCrawlingTagsThumbnail = crawlingTagsThumbnailMapper.selectOne(crawlingTagsThumbnail);
            if (getCrawlingTagsThumbnail == null) {
                crawlingTagsThumbnail.setCreateTime(new Date());
                crawlingTagsThumbnailMapper.insertSelective(crawlingTagsThumbnail);
            }
        }
        return everyOneTags;
    }

    public CrawlingBigPic insertCrawlingBigPic(String everyImageUrl, int pageIndex, String firstImage, String alt, Integer allPage, CrawlingPicThumbnail crawlingPicThumbnail) {
        CrawlingBigPic crawlingBigPic = new CrawlingBigPic();
        crawlingBigPic.setAttributeLink(everyImageUrl);
        crawlingBigPic.setBigPageIndex(pageIndex);
        crawlingBigPic.setBigPicHref(firstImage);
        crawlingBigPic.setThumbnailId(crawlingPicThumbnail.getPicThumbnailId());
        //根据以上条件查询
        CrawlingBigPic getCrawlingBigPic = crawlingBigPicMapper.selectOne(crawlingBigPic);

        crawlingBigPic.setBigPicText(StringUtils.isBlank(alt) ? crawlingPicThumbnail.getPicHrefText() : alt);
        crawlingBigPic.setBigPageSize(allPage);
        crawlingBigPic.setCreateTime(new Date());
        crawlingBigPic.setPicHrefText(crawlingPicThumbnail.getPicHrefText());

        String local_href = getImagePathAndName(everyImageUrl, firstImage);
        if (getCrawlingBigPic == null) {
            String result = HttpClientUtil.downloadFile(firstImage, local_href);
            log.info(crawlingPicThumbnail.getPeopleName() + " 图片下载结果：" + result);
            if (!HttpClientUtil.download.equals(result)) {
                crawlingBigPic.setLocalHref(StringUtils.substringAfter(local_href, IMAGE_PATH));
            }
            crawlingBigPicMapper.insertSelective(crawlingBigPic);
            return crawlingBigPic;
        } else {
            //图片没下载成功的继续下载
            String result = HttpClientUtil.downloadFile(firstImage, local_href);
            log.info(crawlingPicThumbnail.getPeopleName() + " 图片下载结果：" + result);
            if (!HttpClientUtil.download.equals(result)) {
                CrawlingBigPic getCrawlingBigPicInsert = new CrawlingBigPic();
                getCrawlingBigPicInsert.setBigPicId(getCrawlingBigPic.getBigPicId());
                getCrawlingBigPicInsert.setLocalHref(StringUtils.substringAfter(local_href, IMAGE_PATH));
                getCrawlingBigPicInsert.setUpdateTime(new Date());
                crawlingBigPicMapper.updateByPrimaryKeySelective(getCrawlingBigPicInsert);
                return getCrawlingBigPicInsert;
            }
        }
        return null;
    }

    public String getImagePathAndName(String httpUrl, String imgeUrl) {
        if (StringUtils.isNotBlank(httpUrl)) {
            httpUrl = httpUrl.trim();
        }
        if (StringUtils.isNotBlank(imgeUrl)) {
            imgeUrl = imgeUrl.trim();
        }
        httpUrl = httpUrl.trim();
        String regxUrl = "https://www.yeitu.com/";
        String regxImg = "https://img.yeitu.com/";
        String[] strs = StringUtils.substringAfter(httpUrl, regxUrl).split("/");
        StringBuilder detailPath = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            //log.info(strs[i]);
            if (strs[i].indexOf(".html") > 0) {
                break;
            }
            detailPath.append(strs[i]).append("/");

        }
        String[] array = {".jpg", ".png", ".jpeg"};
        for (int i = 0; i < array.length; i++) {
            if (imgeUrl.indexOf(array[i]) > 0) {
                imgeUrl = imgeUrl.substring(0, imgeUrl.indexOf(array[i]) + array[i].length());
                break;
            }
        }

        return IMAGE_PATH + detailPath.toString() + StringUtils.substringAfter(imgeUrl, regxImg);
    }

    /**
     * 方法二：推荐，速度最快
     * 判断是否为整数
     *
     * @param str 传入的字符串
     * @return 是整数返回true, 否则返回false
     */
    @SuppressWarnings("all")
    public boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }


}
